import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Magazzino extends JFrame{

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		if (args.length>0) { 
			MagazzinoDB.setDatabase(args[0]);
		}
		new Magazzino();
	}
	
	JButton btnCarico, btnScarico, btnRicerca, btnChiudi;
	
	public Magazzino() {
		this.setTitle("Magazzino");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addWindowListener(new Closer());
		
		ClickListener cl = new ClickListener();
		
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(0,1));
		
		btnCarico = new JButton("Carico");
		btnCarico.addActionListener(cl);
		mainPanel.add(btnCarico);
		
		btnScarico = new JButton("Scarico");
		btnScarico.addActionListener(cl);
		mainPanel.add(btnScarico);
		
		btnRicerca = new JButton("Ricerca");
		btnRicerca.addActionListener(cl);
		mainPanel.add(btnRicerca);
		
		btnChiudi = new JButton("Chiudi");
		btnChiudi.addActionListener(cl);
		mainPanel.add(btnChiudi);
		
		this.add(mainPanel);
		this.pack();
		this.setVisible(true);
		this.setBounds(0,0,100, 150);
		this.setLocationRelativeTo(null);
	}
	
	private class ClickListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnCarico) {
				new Carico(Magazzino.this);
			}
			if (e.getSource()== btnScarico) {
				new Scarico(Magazzino.this);
			}
			if (e.getSource()==btnRicerca) {
				new Ricerca(Magazzino.this);
			}
			if (e.getSource()==btnChiudi) { 
				JOptionPane.showMessageDialog(null, "Il lavoro e' stato salvato correttamente, arrivederci!","Chiusura...",JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
		}
		
	}
	
	private class Closer implements WindowListener {

		@Override
		public void windowOpened(WindowEvent e) {
			
		}

		@Override
		public void windowClosing(WindowEvent e) {
			JOptionPane.showMessageDialog(null, "Il lavoro e' stato salvato correttamente, arrivederci!","Chiusura...",JOptionPane.INFORMATION_MESSAGE);
			System.exit(0);
		}

		@Override
		public void windowClosed(WindowEvent e) {
			
		}

		@Override
		public void windowIconified(WindowEvent e) {
			
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			
		}

		@Override
		public void windowActivated(WindowEvent e) {
			
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
			
		}
		
	}

}
